package com.ebraga.trackerclub.android.handler.core;

import com.ebraga.trackerclub.android.dao.entity.PositionAndroid;
import com.ebraga.trackerclub.android.dao.entity.UserAndroid;
import com.ebraga.trackerclub.android.exception.BackofficeDBConnectionException;
import com.ebraga.trackerclub.android.exception.handler.BackofficeHandlerException;
import com.ebraga.trackerclub.android.exception.manager.BackofficeIllegalArgumentManagerException;
import com.ebraga.trackerclub.android.exception.manager.BackofficeInvalidSessionManagerException;
import com.ebraga.trackerclub.android.exception.manager.BackofficeManagerException;
import com.ebraga.trackerclub.android.handler.api.PositionHandler;
import com.ebraga.trackerclub.android.manager.api.PositionManager;
import com.ebraga.trackerclub.android.manager.api.UserManager;
import com.ebraga.trackerclub.android.util.DLog;
import com.ebraga.trackerclub.android.vo.PositionVO;

public abstract class PositionHandlerImpl implements PositionHandler {
	
	protected PositionManager positionManager;
	protected UserManager userManager;

	@Override
	public void sendPositionData( PositionVO positionVO) throws BackofficeHandlerException {
		
		UserAndroid user;
		PositionAndroid position;
				
		if (positionVO == null){
			throw new BackofficeHandlerException("Posição vazia"); 
		}
		
		if (positionVO.getImei() == null){
			throw new BackofficeHandlerException("Usuario não informado para a posição"); 
		}
		
		try{
			user = userManager.getUser(positionVO.getImei());
		}catch(BackofficeIllegalArgumentManagerException e){
			DLog.e(e);
			throw new BackofficeHandlerException("Usuario invalido"); 
		} catch (BackofficeDBConnectionException e) {
			DLog.e(e);
			throw new BackofficeHandlerException("Erro ao conectar banco de dados");
		} catch (BackofficeManagerException e) {
			DLog.e(e);
			throw new BackofficeHandlerException("Erro ao recuperar usuario");
		}
		
		try{		
			
			position = parsePositionVOToEntity (positionVO);

			positionManager.sendPositionData(user, position);
		}catch(BackofficeIllegalArgumentManagerException i){
			DLog.e(i);
			throw new BackofficeHandlerException("Argumentos Usuario ou Posição invalidos");
		}catch(BackofficeDBConnectionException db){
			DLog.e(db);
			throw new BackofficeHandlerException("Erro ao conectar Banco de Dados");
		}catch(BackofficeInvalidSessionManagerException db){
			DLog.e(db);
			throw new BackofficeHandlerException("Erro no Contexto Android");
		}catch(BackofficeManagerException me){
			DLog.e(me);
			throw new BackofficeHandlerException("Erro ao inserir Posição");
 		}
	}
		
	public PositionAndroid parsePositionVOToEntity (PositionVO positionVO){
		PositionAndroid position = new PositionAndroid();
		
		position.setImei(positionVO.getImei());
		
		if (positionVO.getAccuracy() == null){
			position.setAccuracy(null);
		}else{
			position.setAccuracy(Double.parseDouble(positionVO.getAccuracy()));
		}
		
		if (positionVO.getSpeed() == null){
			position.setSpeed(null);
		}else{
			position.setSpeed(Double.parseDouble(positionVO.getSpeed()));
		}		
		
		if(positionVO.getLatitude() == null){
			position.setLatitude(null);
		}else{
			position.setLatitude(Double.parseDouble(positionVO.getLatitude()));
		}
		
		if (positionVO.getLongitude() == null){
			position.setLongitude(null);
		}else{
			position.setLongitude(Double.parseDouble(positionVO.getLongitude()));
		}
		
		if (positionVO.getProvider() == null){
			position.setProvider(null);
		}else{
			position.setProvider(positionVO.getProvider());
		}
		
		
		
		if (positionVO.getDateLastPosition() == null){
			position.setDateCreated(null);
		}else{
			position.setDateCreated(positionVO.getDateLastPosition().getTime());
		}
		return position;
	}
	
	protected void setPositionManager(PositionManager positionManager) {
		this.positionManager = positionManager;
	}
	
	protected void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}	
}
